import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Layout from '@/layout/index.vue'

// 公共路由
export const constantRoutes: RouteRecordRaw[] = [
  { path: '/:pathMatch(.*)*', redirect: '/404' },
  {
    path: '/401',
    name: 'error',
    component: () => import('@/views/error/401.vue')
  },
  {
    path: '/404',
    name: 'notFound',
    component: () => import('@/views/error/404.vue')
  },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index.vue'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/forms',
    name: 'Forms',
    component: Layout,
    meta: { title: '表单管理', icon: '' },
    redirect: { name: 'FormsList' },
    children: [
      {
        path: 'formsList',
        component: () => import('@/views/forms/index.vue'),
        name: 'FormsList',
        meta: { title: '表单列表', icon: '' }
      },
      {
        path: 'formsDetail',
        component: () => import('@/views/forms/detail/index.vue'),
        name: 'FormsDetail',
        meta: { title: '表单列表', icon: '' }
      }
    ]
  },
  {
    path: '/bpmnProcess',
    name: 'BpmnProcess',
    component: Layout,
    meta: { title: '流程管理', icon: '' },
    redirect: { name: 'BpmnMonitorList' },
    children: [
      {
        path: 'bpmnMonitorList',
        component: () => import('@/views/bpmnProcess/index.vue'),
        name: 'BpmnMonitorList',
        meta: { title: '流程监控', icon: '' }
      },
      {
        path: 'bpmnDesign',
        component: () => import('@/views/bpmnProcess/bpmnDesign/index'),
        name: 'BpmnDesign',
        meta: { title: '流程设计', icon: '' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router
